<template>
    <el-main>
        <!-- 查询条件 -->
        <el-form :model="searchModel" ref="searchForm" label-width="80px" :inline="true" size="small">
            <el-form-item>
                <el-input v-model="searchModel.key" placeholder="请输入" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain icon="el-icon-search" @click="search(pageNo, pageSize)">查询
                </el-button>
                <el-button icon="el-icon-refresh-right" @click="resetValue">重置</el-button>
                <el-button type="success" plain icon="el-icon-plus" @click="openAddWindow()">新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 数据表格 -->
        <el-table :data="payList" :height="tableHeight" border stripe style="width: 100%; margin-bottom: 10px">
            <el-table-column prop="id" label="编号" width="100" align="center"></el-table-column>
            <el-table-column prop="notes" label="支出说明"></el-table-column>
            <el-table-column prop="quantity" label="支出金额"></el-table-column>
            <el-table-column prop="time" label="支出时间"></el-table-column>
            <el-table-column prop="deptName" label="支出部门"></el-table-column>
            <el-table-column label="操作" align="center" width="290">
                <template slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" plain size="small" @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button icon="el-icon-delete" type="danger" plain size="small"
                        @click="handleDelete(scope.row)">删除
                    </el-button>

                </template>
            </el-table-column>
        </el-table>
        <!-- 分页工具栏 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加和修改支出窗口 -->
        <system-dialog :title="payDialog.title" :visible="payDialog.visible" :width="payDialog.width"
            :height="payDialog.height" @onClose="onClose" @onConfirm="onConfirm">
            <div slot="content">
                <el-form :model="pay" ref="payForm" :rules="rules" label-width="80px" :inline="false" size="small">
                    <el-form-item label="支出说明" prop="notes">
                        <el-input v-model="pay.notes"></el-input>
                    </el-form-item>
                    <el-form-item label="支出金额" prop="quantity">
                        <el-input v-model="pay.quantity"></el-input>
                    </el-form-item>
                    <el-form-item label="支出时间" prop="time">
                        <el-date-picker v-model="pay.time" type="date" placeholder="请选择日期" format="yyyy/MM/dd"
                            value-format="yyyy-MM-dd hh:mm:ss" />
                    </el-form-item>
                    <el-form-item label="支出部门" prop="deptId">
                        <el-select placeholder="请选择部門" clearable v-model="pay.deptId">
                            <el-option v-for="item in employeeOptions" :key="item.id" :label="item.departmentName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
        </system-dialog>
        <!-- 分配权限树窗口 -->
        <system-dialog :title="assignDialog.title" :visible="assignDialog.visible" :width="assignDialog.width"
            :height="assignDialog.height" @onClose="onAssignClose" @onConfirm="onAssignConfirm">
            <div slot="content">
                <el-tree ref="assignTree" :data="assignTreeData" node-key="id" :props="defaultProps" empty-text="暂无数据"
                    :show-checkbox="true" :highlight-current="true" default-expand-all></el-tree>
            </div>
        </system-dialog>
    </el-main>
</template>

<script>
//导入对话框组件
import deptApi from "@/api/department";
import propertyApi from "@/api/notice";

import SystemDialog from "@/components/system/SystemDialog.vue";
export default {
    name: "payList",
    components: {
        SystemDialog,
    },

    data() {
        return {
            //分配权限窗口属性
            employeeOptions: [],
            assignDialog: {
                title: "",
                visible: false,
                height: 450,
                width: 300,
            },
            payId: "", //支出ID
            assignTreeData: [], //树节点数据
            //树节点属性
            defaultProps: {
                children: "children",
                label: "label",
            },
            //查询条件
            searchModel: {
                key: "",
                pageNo: 1,
                pageSize: 10,
                userId: this.$store.getters.userId, //用户ID
            },
            rules: {
                notes: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                quantity: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                time: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
                departmentName: [
                    { required: true, trigger: "blur", message: "请输入" },
                ],
            },
            //添加和修改支出窗口属性
            payDialog: {
                title: "",
                visible: false,
                height: 230,
                width: 500,
            },
            //支出对象
            pay: {
                id: "",
                departmentName: null,
                deptId: null,
                time: "",
                notes: "",
                quantity: "",
                createUser: this.$store.getters.userId,
            },
            payList: [], //数据列表
            tableHeight: 0, //表格高度
            pageNo: 1, //当前页码
            pageSize: 10, //每页显示数量
            total: 0, //总数量
        };
    },
    methods: {
        /**
  * 获取部門名稱
  */
        async getStaff() {
            //发送查询请求
            let res = await deptApi.getDeptList({});
            //执行成功
            if (res.success) {
                console.log(res)
                //员工列表
                this.employeeOptions = res.data;
                // console.log(this.employeeOptions)
            }
        },
        /**
         * 查询
         */
        async search(pageNo = 1, pageSize = 10) {
            this.searchModel.pageNo = pageNo; //当前页码
            this.searchModel.pageSize = pageSize; //每页显示数量
            // this.searchModel.key = this.notes
            //发送查询请求
            let res = await propertyApi.FinancialExpensesList(this.searchModel);
            //执行成功
            if (res.success) {
                //支出列表
                this.payList = res.data.records;

            }
        },
        /**
         * 重置查询条件
         */
        resetValue() {
            //清空查询条件
            this.searchModel.key = "";
            //重新查询
            this.search();
        },
        /**
         * 当每页数量发生变化时触发该事件
         */
        handleSizeChange(size) {
            this.pageSize = size; //将每页显示的数量交给成员变量
            this.search(this.pageNo, size);
        },
        /**
         * 当页码发生变化时触发该事件
         */
        handleCurrentChange(page) {
            this.pageNo = page;
            //调用查询方法
            this.search(page, this.pageSize);
        },
        /**
         * 打开添加窗口
         */
        openAddWindow() {
            //清空表单数据
            this.$resetForm("payForm", this.pay);
            this.payDialog.title = "新增支出"; //设置窗口标题
            this.payDialog.visible = true; //显示窗口
        },
        /**
         * 窗口取消事件
         */
        onClose() {
            this.payDialog.visible = false;
        },

        /**
         * 窗口确认事件
         */
        onConfirm() {
            //表单验证
            this.$refs.payForm.validate(async (valid) => {
                if (valid) {
                    let res = null;
                    //判断支出ID是否为空
                    res = await propertyApi.FinancialExpensesSave(this.pay);
                    //判断是否成功
                    if (res.success) {
                        this.$message.success(res.message);
                        //刷新
                        this.search(this.pageNo, this.pageSize);
                        //关闭窗口
                        this.payDialog.visible = false;
                    } else {
                        this.$message.error(res.message);
                    }
                }
            });
        },
        /**
         * 打开编辑窗口
         */
        handleEdit(row) {
            //数据回显
            this.$objCopy(row, this.pay); //将当前编辑的数据复制到pay对象中
            //设置窗口标题
            this.payDialog.title = "编辑支出";
            //显示编辑支出窗口
            this.payDialog.visible = true;
        },


        /**
        * 删除支出
        * @param row
        */
        async handleDelete(row) {
            //查询该支出是否被使用
            let res = await propertyApi.FinancialExpensesDel({ mainIds: [row.id] });
            if (res.success) {
                //成功提示
                this.$message.success(res.message);

                //刷新
                this.search(this.pageNo, this.pageSize);
            } else {
                //失败提示
                this.$message.error(res.message);
            }
        }


    },
    created() {
        //调用查询支出列表的方法
        this.search();
    },
    mounted() {
        this.$nextTick(() => {
            this.tableHeight = window.innerHeight - 220;
            this.getStaff();
        });
    },
};
</script>

<style></style>